#include "lisounddeviceinfo.h"

LiSoundDeviceInfo::LiSoundDeviceInfo(QObject *parent) : QObject(parent)
{

}

QString LiSoundDeviceInfo::getDeviceNameArray()
{
    QList<QAudioDeviceInfo> tempDeviceInfoList=
            QAudioDeviceInfo::availableDevices (QAudio::AudioInput);
    qDebug()<<"ListCount:"<<tempDeviceInfoList.count ();
    QJsonArray deviceNameArray;
    foreach (QAudioDeviceInfo tempDeviceInfoeach, tempDeviceInfoList) {
        QJsonObject tempJsonObject;
        tempJsonObject.insert ("name",tempDeviceInfoeach.deviceName ());
        deviceNameArray.append (QJsonValue(tempJsonObject));
    }
    if(deviceNameArray.count ()>0){
        QJsonDocument jsondoc;
        jsondoc.setArray(deviceNameArray);
        return jsondoc.toJson ();
    }
    else {
        return "";
    }
}

QAudioDeviceInfo LiSoundDeviceInfo::getDeviceInfoObject(int index)
{
    QList<QAudioDeviceInfo> tempDeviceInfoList=
            QAudioDeviceInfo::availableDevices (QAudio::AudioInput);
    return tempDeviceInfoList.at (index);
}

QString LiSoundDeviceInfo::objectArraytoJson(QString name,QJsonArray &jsonArray)
{
    QJsonObject tempObject;
    tempObject.insert (name,QJsonValue(jsonArray));
    QJsonDocument jsondoc;
    jsondoc.setObject (tempObject);
    return jsondoc.toJson ();
}

QString LiSoundDeviceInfo::getDeviceByteOrdersObject (int index)
{
    QJsonArray tempArray;
    QList<QAudioFormat::Endian> tempList=getDeviceInfoObject (index).supportedByteOrders ();
    foreach (QAudioFormat::Endian en, tempList) {
        switch (en) {
        case QAudioFormat::BigEndian:
            tempArray.append (QJsonValue("BIGEndian"));
            break;
        case QAudioFormat::LittleEndian:
            tempArray.append (QJsonValue("LittleEndian"));
            break;
        default:
            tempArray.append (QJsonValue("NoKnowEndian"));
            break;
        }
    }
    return objectArraytoJson ("ByteOrders",tempArray);
}

QString LiSoundDeviceInfo::getDeviceChannelCountsObject (int index)
{
    QJsonArray tempArray;
    QList<int> tempList=getDeviceInfoObject (index).supportedChannelCounts ();
    foreach (int i, tempList) {
        tempArray.append (QJsonValue(i));
    }
    return objectArraytoJson ("Channel",tempArray);
}

QString LiSoundDeviceInfo::getDeviceCodecsObject (int index)
{
    QJsonArray tempArray;
    QStringList tempList=getDeviceInfoObject (index).supportedCodecs ();
    foreach (QString str, tempList) {
        qDebug()<<"code"<<str;
        tempArray.append (QJsonValue(QString(str)));
    }
    return objectArraytoJson ("Codecs",tempArray);
}

QString LiSoundDeviceInfo::getDeviceSampleRatesObject (int index)
{
    QJsonArray tempArray;
    QList<int> tempList=getDeviceInfoObject (index).supportedSampleRates ();
    foreach (int i, tempList) {
        tempArray.append (QJsonValue(i));
    }
    return objectArraytoJson ("SampleRates",tempArray);
}

QString LiSoundDeviceInfo::getDeviceSampleSizesObject (int index)
{
    QJsonArray tempArray;
    QList<int> tempList=getDeviceInfoObject (index).supportedSampleSizes ();
    foreach (int i, tempList) {
        tempArray.append (QJsonValue(i));
    }
    return objectArraytoJson ("SampleSizes",tempArray);
}

QString LiSoundDeviceInfo::getDeviceSampleTypesObject (int index)
{
    QJsonArray tempArray;
    QList<QAudioFormat::SampleType> tempList=getDeviceInfoObject (index).supportedSampleTypes ();
    foreach (QAudioFormat::SampleType en, tempList) {
        switch (en) {
        case QAudioFormat::Unknown:
            tempArray.append (QJsonValue("Unknown"));
            break;
        case QAudioFormat::SignedInt:
            tempArray.append (QJsonValue("SignedInt"));
            break;
        case QAudioFormat::UnSignedInt:
            tempArray.append (QJsonValue("UnSignedInt"));
            break;
        case QAudioFormat::Float:
            tempArray.append (QJsonValue("Float"));
            break;
        default:
            tempArray.append (QJsonValue("NO"));
            break;
        }
    }
    return objectArraytoJson ("SampleTypes",tempArray);
}
